import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Glob here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Glob  extends Enemy
{
    GreenfootImage buffer = new GreenfootImage(11,12);
    GreenfootImage globs = new GreenfootImage(11,12);
    Zony z;
    int counter=0,destX,destY=0,y,dir=-1,health=3,hit=0;
    double a,b,velX=0,velY,x;
    boolean chasing=false;
    
    public Glob(){
        setImage(buffer);
    }
    
    
    protected void addedToWorld(World world)
    {
        globs=((it)getWorld()).getGlob();  
        buffer.drawImage(globs,0,0);
        Zony it = (Zony) getWorld().getObjects(Zony.class).get(0);
        z=it;
        x=getX();
        y=getY();
    }
    
    
    public void act() 
    {
        buffer.clear();
        if(counter==20){
             
             destination();
             destY=y;
             velX = x/20.0;
             x=getX();
             y=getY();
             a = (destY+100)/200.0;
             b = (-destY-200)/20.0;
             counter=-Greenfoot.getRandomNumber(20);
             if(!chasing)counter=counter-10;
            }else counter++;
            
        velY = a*counter*counter + b*counter;
        if(counter>0){
            x=x+velX;
            setLocation((int)(x+.1),y+(int)velY);
        }
        
        if(counter>10 || counter<0)
            buffer.drawImage(globs,0,0);
        else
            buffer.drawImage(globs,-11,0);
        if(velX>0)buffer.mirrorHorizontally();  
        if(hit%2 == 1)buffer.clear();
        if(hit!=0)hit--;
    }   
    
    
//**********************************************************************    
//**********************************************************************     
 
    
    public void destination(){
        int yTemp=0,xTemp=0,max;
        if(Math.abs(z.getX()-getX())<100 && Math.abs(z.getY()-getY())<100)
            chasing = true;
        if(Math.abs(z.getX()-getX())>200 || Math.abs(z.getY()-getY())>100)
            chasing = false;
        if(chasing){
            dir=1;
            if(z.getX()<getX())dir=-1;
        }
        
        
        if(chasing){
            x=16*dir;
            max = 40*dir+4*dir;
            while(getX()+max>=599){ max=max-4; }
            while(getX()+max<=0){ max=max+4; }  
            while(getX()+x>=560){ x=x-4; }
            while(getX()+x<=40){ x=x+4; }  
            while(x!=max){
                x=x+4*dir;
                y=-48;
                while(y!=32 && getY()+y<380){
                    y=y+4;  
                    if(valid((int)x,y) && z.getY()>getY() && y>=yTemp){
                       yTemp=y;
                       xTemp=(int)x;
                    }
                    if(valid((int)x,y) && z.getY()<getY() && y<=yTemp){
                       yTemp=y;
                       xTemp=(int)x;
                    }
                }
                  
            }
            
        }
        if(chasing){
            x=xTemp;
            y=yTemp; 
            if(xTemp==0)chasing=false;
        }
        
        
        if(!chasing){
            x=40*dir+4*dir;
            while(getX()+x>=580){ x=x-4; }
            while(getX()+x<=20){ x=x+4; }  
            y=-32;
            while(!valid((int)x,y) && x!=0){
                x=x-4*dir;
                y=-32;
                    while(!valid((int)x,y) && y!=32 && getY()+y<380){
                        y=y+4;  
                    }
            }
        }             
  
        if(!chasing && x==0)dir=-dir;
        y--;
        while(((it)getWorld()).ok(getX()+(int)x,getY()+y+6)){
            y++;
        }
        
 
    }
    
    public boolean valid(int X,int Y){
        return ((it)getWorld()).ok(getX()+X,getY()+Y+6) && !((it)getWorld()).ok(getX()+X,getY()+Y+10);
    }
    
    public void hurt(int l){
        double ang,amp;
        if(hit==0)
            health=health-l;
        hit=10;    
        if(health<1){
            for(int f=0;f<10;f++){
                 ang = 3.14159*Math.random();
                 amp = (Math.random()+1);
                 getWorld().addObject(new projectile(6,amp*Math.cos(ang),-amp*Math.sin(ang),true),getX(),getY());
             }
            ((it)getWorld()).removeObject(this);
        }
    }
    
   
            
    
  
}
